/*
 * LoginFrame.java
 *
 * Created on March 25, 2007, 8:39 PM
 */

package edu.byu.isys413.group1E.gui;

import edu.byu.isys413.group1E.*;
import edu.byu.isys413.group1E.data.*;
import java.applet.Applet;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.*;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

/**
 *
 * @author  tkulbeth
 */
public class LoginFrame extends javax.swing.JFrame {
    
    StoreBO store = null;
    
    /**
     * Creates new form LoginFrame
     */
    public LoginFrame() {
        initComponents();
        this.field_uName.setText("");
        this.field_pwd.setText("");
        this.field_pwd.addKeyListener(new enterListener(this));
        try {
            store = StoreDAO.getInstance().read("0000011106a3cc370000007f000002");
        } catch (ConnectionPoolException ex) {
            ex.printStackTrace();
        } catch (DataException ex) {
            ex.printStackTrace();
        }
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel2 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        button_Login = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        label_uName = new javax.swing.JLabel();
        field_uName = new javax.swing.JTextField();
        label_pwd = new javax.swing.JLabel();
        field_pwd = new javax.swing.JPasswordField();

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jButton1.setText("jButton1");
        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Login");
        setName("LoginFrame");
        setResizable(false);
        button_Login.setText("Login");
        button_Login.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_LoginActionPerformed(evt);
            }
        });

        jPanel4.add(button_Login);

        getContentPane().add(jPanel4, java.awt.BorderLayout.SOUTH);

        jLabel3.setText("Please enter your Username and Password:");
        getContentPane().add(jLabel3, java.awt.BorderLayout.NORTH);

        jPanel3.setLayout(new java.awt.GridLayout(2, 2, 3, 3));

        label_uName.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        label_uName.setText("Username:");
        jPanel3.add(label_uName);

        field_uName.setText("<username>");
        jPanel3.add(field_uName);

        label_pwd.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        label_pwd.setText("Password:");
        jPanel3.add(label_pwd);

        field_pwd.setText("jPasswordField1");
        jPanel3.add(field_pwd);

        getContentPane().add(jPanel3, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void button_LoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_LoginActionPerformed
        try {
            EmployeeBO emp = EmployeeDAO.getInstance().readByUserName(this.field_uName.getText());
            System.out.println(this.field_uName.getText());
            System.out.println(this.field_pwd.getPassword());
            if(emp.validatePassword(this.field_pwd.getPassword()) == false){
                this.field_uName.setText("");
                this.field_pwd.setText("");
                JOptionPane.showMessageDialog(null,"The password you entered was incorrect.  Please try again.");
            }else{
                try {                                                           //change appearance according to system
                    String look = UIManager.getSystemLookAndFeelClassName();
                    UIManager.setLookAndFeel(look);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                Session s = Session.create(emp, store);
                MainFrame mFrame = new MainFrame(s);                           //initialize frame
                mFrame.setLocationRelativeTo(null);                                       //set frame location to center of screen
                mFrame.setVisible(true);                                                  //display frame
                this.dispose();
            }
        } catch (ConnectionPoolException ex) {
            JOptionPane.showMessageDialog(null,"An error occurred when connecting to the database.");
        } catch (DataException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null,"The User ID entered was incorrect.");
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_button_LoginActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {                                                           //change appearance according to system
                    String look = UIManager.getSystemLookAndFeelClassName();
                    UIManager.setLookAndFeel(look);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                LoginFrame login = new LoginFrame();
                login.setLocationRelativeTo(null);
                login.setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton button_Login;
    private javax.swing.JPasswordField field_pwd;
    private javax.swing.JTextField field_uName;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JLabel label_pwd;
    private javax.swing.JLabel label_uName;
    // End of variables declaration//GEN-END:variables
    
    public class enterListener extends Applet implements KeyListener {
        LoginFrame LF = null;
        public enterListener(LoginFrame lf){
            this.LF = lf;
        }
        public void keyTyped(KeyEvent e) {}
        public void keyReleased(KeyEvent e) {}
        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
            if (key == KeyEvent.VK_ENTER) {
                try {
                    EmployeeBO emp = EmployeeDAO.getInstance().readByUserName(LF.field_uName.getText());
                    System.out.println(LF.field_uName.getText());
                    System.out.println(LF.field_pwd.getPassword());
                    if(emp.validatePassword(LF.field_pwd.getPassword()) == false){
                        LF.field_uName.setText("");
                        LF.field_pwd.setText("");
                        JOptionPane.showMessageDialog(null,"The password you entered was incorrect.  Please try again.");
                    }else{
                        try {                                                           //change appearance according to system
                            String look = UIManager.getSystemLookAndFeelClassName();
                            UIManager.setLookAndFeel(look);
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                        Session s = Session.create(emp, store);
                        MainFrame mFrame = new MainFrame(s);                           //initialize frame
                        mFrame.setLocationRelativeTo(null);                                       //set frame location to center of screen
                        mFrame.setVisible(true);                                                  //display frame
                        LF.dispose();
                    }
                } catch (ConnectionPoolException ex) {
                    JOptionPane.showMessageDialog(null,"An error occurred when connecting to the database.");
                } catch (DataException ex) {
                    ex.printStackTrace();
                    JOptionPane.showMessageDialog(null,"The User ID entered was incorrect.");
                } catch (SQLException ex) {
                    ex.printStackTrace();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                
            }//if
        }//keyPressed
    }//enterListener
    
}
